import { onLoad, onShow } from '@dcloudio/uni-app';
import { useGlobalStore } from '@/service/store/global-store';
import { usePageGuard } from '@/service/helper';
import { useProvideConfigProvider } from './context';
import { ref } from 'vue';
import { setGlobalModal } from '@jecloud/utils';

export function useConfigProvider({ props, context }) {
  const { expose } = context;
  const store = useGlobalStore();
  const { tokenGuard, i18nGuard, themeGuard } = usePageGuard();
  const modalRef = ref();
  const $config = {};
  // 暴露对象
  expose($config);
  useProvideConfigProvider($config);

  // 页面load守卫
  onLoad(function () {
    props.tokenGuard && tokenGuard();
  });

  // 更新标题栏背景色
  onShow(() => {
    // 国际化，处理页面标题
    props.i18nTitle && i18nGuard();
    // 暗色主题，更新标题栏样式
    themeGuard();
    // 设置全局modal
    setGlobalModal(modalRef);
  });
  return { store, modalRef };
}
